package com.atguigu4.search_sort.exer1;

/**
 * @author lhl
 * @date 2025/3/31 13:55
 */
public class ArrayExer01_2 {
    public static void main(String[] args) {
        int[] arr={1,2,3,4,5,6,7};

        int deleteIndex = 4;

        //方式1：不新建数组
//        for (int i = deleteIndex; i < arr.length - 1; i++) {
//            arr[i] = arr[i + 1];
//        }
//
//        //修改最后的元素,设置为默认值
//        arr[arr.length - 1] = 0;


        //方式2：新建数组，新数组长度比原有数组少1个
        int[] newArr = new int[arr.length - 1];
        for (int i = 0; i < deleteIndex; i++) {
            newArr[i] = arr[i];
        }

        for (int i = deleteIndex; i < arr.length - 1 ; i++) {
            newArr[i] = arr[i + 1];
        }
        arr = newArr;

        for (int i = 0; i < arr.length; i++) {
            System.out.print(arr[i]+"\t");
        }

    }
}
